package com.qf.service.impl;

import com.qf.dao.RotationImageRepository;
import com.qf.pojo.entity.RotationImage;
import com.qf.pojo.req.RotationImageReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.RotationImageService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Api("轮播图接口实现类")
@Service
public class RotationImageServiceImpl implements RotationImageService {
    @Autowired
    RotationImageRepository rotationImageRepository;
    @Override
    public BaseResp findAll(Integer page, Integer size) {
        //通过分页查询
        PageRequest of = PageRequest.of(page - 1, size);
        Page<RotationImage> all = rotationImageRepository.findAll(of);
        List<RotationImage> content = all.getContent();
        long totalElements = all.getTotalElements();
        return new BaseResp(1,"success",content,totalElements);
    }

    @Override
    public BaseResp updateOrSave(RotationImageReq rotationImageReq) {
        RotationImage rotationImage = new RotationImage();
        BeanUtils.copyProperties(rotationImageReq,rotationImage);
        rotationImageRepository.saveAndFlush(rotationImage);
        return new BaseResp().SUCCESS();
    }

    @Override
    public BaseResp deleteById(Integer id) {
        rotationImageRepository.deleteById(id);
        return new BaseResp().ERROR();
    }
}
